using System;
using System.IO;
using System.Configuration;

namespace Irc.Net
{
	public sealed class DebugLogger : IDisposable
	{
		private static DebugLogger instance = new DebugLogger();
		public static DebugLogger Instance {
			get {
				return instance;
			}
		}
		StreamWriter log = null;
		private DebugLogger()
		{
			log = new StreamWriter(File.Open(ConfigurationManager.AppSettings["DebugLog"], FileMode.Append));
		}
		public void AddMessage(string message, DebugLevel level)
		{
			if(log.BaseStream.CanWrite)
			{
				if(level >= (DebugLevel)DebugLevel.Parse(typeof(DebugLevel), ConfigurationManager.AppSettings["DebugLevel"], true))
					return;
				log.WriteLine(String.Format(Strings.LogMessage,
				                  DateTime.Now.ToUniversalTime(),
				                  Enum.Format(typeof(DebugLevel), level, "G"),
								  new System.Diagnostics.StackTrace(false).GetFrame(1).GetMethod().DeclaringType.FullName,
				                  message));
				log.Flush();
			}
		}
		public void Dispose()
		{
			log.Flush();
			log.Close();
		}
		public bool IsOpen { get { return log.BaseStream.CanWrite; } }
	}
	public enum DebugLevel {
		None,
		CriticalError,
		Error,
		Warning,
		Info,
		Message,
		All
	}
}
